<?php

/*
	Class: App_Email

	About: Author
		J William McCarthy

	About: License
		<http://communit.as/docs/license>
*/
class App_Email {

	var $_template_path = null;
	var $_view = null;

	/* Group: Constructors */

	/*
		Constructor: App_Email
			Sets a template path variable (_template_path), and instantiates a logger.
	*/
	function App_Email() {
		$this->_template_path = Zend_Registry::get('theme_global_path') . "/email/";
		$renderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
		$this->_view = $renderer->view;
		$this->_logger = App_Registry::get('logger');
	}

	/*
		Function: sendEmail
			Sends an email.

		Arguments:
			subject - The subject line of the email to send.
			to_address - The recipient email address to send the email to.
			template - A template file to use for the layout.
			params (optional) - An array of arbitrary parameters that get rendered as view variables, each of which can be used within the supplied template.
			to_name (optional) - The name of the recipient.
			isHtml (optional) - Sets whether the email is plain text (false) or HTML (true). Default is false.
	*/
	function sendEmail($subject, $to_address, $template, $params = null, $to_name = null, $isHtml = false) {
		$useAuth = App_Registry::get('smtp_use_auth', 0);

		if (array_key_exists('from_email', $params)) {
			$site_from_email = $params['from_email'];
		} else {
			$site_from_email = App_Registry::get('site_from_email', null);
		}

		// TODO - shouldn't this be from_name instead of from_email ?
		if (array_key_exists('from_email', $params)) {
			$site_from = $params['from_name'];
		} else {
			$site_from = App_Registry::get('site_from', null);
		}

		$smtp = App_Registry::get('smtp_server', null);
		$username = App_Registry::get('smtp_username', null);
		$password = App_Registry::get('smtp_password', null);
		$ssl = App_Registry::get('smtp_ssl_type', null);  //tls

		$config = array();
		if ($useAuth == 1) {
			$config = array('auth' => 'login',
				'username' => $username,
				'password' => $password,
				'ssl' => $ssl);
		}
		try {
		$tr = new Zend_Mail_Transport_Smtp($smtp, $config);
		Zend_Mail::setDefaultTransport($tr);

		$mail = new Zend_Mail();
		foreach ($params as $key => $value) {
			$this->_view->__set($key, $value);
		}
		$message = $this->_view->render("file:" . $this->_template_path . $template);

		if ($isHtml) {
			$mail->setBodyHtml($message);
		} else {
			$mail->setBodyText($message);
		}

		$mail->setFrom($site_from_email, $site_from);
		if (!is_null($to_name) && trim($to_name) != '') {
			$mail->addTo($to_address, $to_name);
		} else {
			$mail->addTo($to_address);
		}
		$mail->setSubject($subject);
		$mail->setReturnPath(App_Registry::get('site_from_email', 'nobody@nowhere.com'));
		$id_part = substr($site_from_email, strpos('@', $site_from_email));
		$message_id = md5(uniqid()) . $id_part;
		$mail->addHeader('Message-Id', $message_id);
		$mail->send();
		} catch (Exception $e) {
			$this->_logger->report('email: could not send', $e, Zend_Log::ERR);
		}
	}

	/*
		Function: sendHtmlEmail
			Sends an HTML email. The same as using sendEmail with the isHtml boolean set to true (in fact, that's all this does).

		Arguments:
			subject - The subject line of the email to send.
			to_address - The recipient email address to send the email to.
			template - A template file to use for the layout.
			params (optional) - 
			to_name (optional) - 

		See Also:
			- <sendEmail>
	*/
	function sendHtmlEmail($subject, $to_address, $template, $params = null, $to_name = null) {
		$this->sendEmail($subject, $to_address, $template, $params, $to_name, true);
	}

}
